package org.sokolenko.regexp;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.DialogBuilder;
import com.intellij.openapi.ui.popup.*;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.editor.Editor;
import com.intellij.peer.PeerFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.PopupHandler;

import javax.swing.*;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Anatoliy Sokolenko
 *         1 Aug 2009 1:08:09
 */
public class EvaluationForm {
    private JPanel contentPanel;
    private JTextField expressionField;
    private JTextArea inputArea;
    private JTextArea outputArea;
    private JButton evaluateButton;

    private Project project;

    public EvaluationForm(Project project) {
        this.project = project;
        initialize();
    }

    protected void initialize() {
        evaluateButton.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                EvaluationData data = getData(  );
                String output =  processExpression( data.getExpression(), data.getInput() );
                data.setOutput( output );
                setData( data );
            }
        });
    }

    private String processExpression( String expression, String input ) {
        StringBuilder sb = new StringBuilder();

        Pattern pattern = Pattern.compile( expression );
        Matcher matcher = pattern.matcher( input );

        for ( int i = 0; matcher.find(); i++ ) {
            sb.append( "Match: " );
            sb.append( i );
            sb.append( "\n" );

            for ( int group = 0; group < matcher.groupCount(); group++ ) {
                sb.append( "   group " );
                sb.append( group );
                sb.append( " = " );
                sb.append( matcher.group( group ) );
                sb.append( "\n" );
            }
        }

        return sb.toString();
    }

    public void show( Editor editor ) {
        ComponentPopupBuilder builder = JBPopupFactory.getInstance().createComponentPopupBuilder( contentPanel, null );
        builder.setProject( project );
        builder.setTitle( "Some popup" );
        builder.setMovable( true );
        builder.setFocusable( true );
        builder.setResizable( true );
        JBPopup popup = builder.createPopup();
        popup.showInBestPositionFor( editor );
    }

    public void setData( EvaluationData data ) {
        expressionField.setText( data.getExpression() );
        inputArea.setText( data.getInput() );
        outputArea.setText( data.getOutput() );
    }

    public void getData( EvaluationData data ) {
        data.setExpression( expressionField.getText() );
        data.setInput( inputArea.getText() );
        data.setOutput( outputArea.getText() );
    }

    public EvaluationData getData() {
        EvaluationData data = new EvaluationData();
        getData( data );
        return data;
    }

    public boolean isModified( EvaluationData data ) {
        if ( expressionField.getText() != null ? !expressionField.getText().equals( data.getExpression() ) : data.getExpression() != null )
            return true;
        if ( inputArea.getText() != null ? !inputArea.getText().equals( data.getInput() ) : data.getInput() != null )
            return true;
        if ( outputArea.getText() != null ? !outputArea.getText().equals( data.getOutput() ) : data.getOutput() != null )
            return true;
        return false;
    }
}
